package dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import pojo.Account;
import util.MySqlDataAccessHelper;

public class AccountDAO {
	String connectionString;
	String username;
	String password;
	public AccountDAO(String c,String u,String p) {
		this.connectionString = c;
		this.username = u;
		this.password = p;
	}
	public Account getUser (String name){
		Account user = null;
		String query = "select * from account  where username='" + name+"'";
		MySqlDataAccessHelper helper = new MySqlDataAccessHelper(connectionString,username,password);
		helper.open();
		ResultSet rs = helper.executeQuery(query);
		try {
			while (rs.next()) {
				user = new Account();
				user.setId(rs.getInt("id"));
				user.setUsername(rs.getString("username"));
				user.setPassword(rs.getString("password"));
				user.setAvatar(rs.getString("avatar"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		helper.close();
		return user;
	}
	public ArrayList<Account> getListUser (){
		ArrayList<Account> list = new ArrayList<Account>();
		String query = "select * from account " ;
		MySqlDataAccessHelper helper = new MySqlDataAccessHelper(connectionString,username,password);
		helper.open();
		ResultSet rs = helper.executeQuery(query);
		try {
			while (rs.next()) {
				Account user = new Account();
				user.setId(rs.getInt("id"));
				user.setUsername(rs.getString("username"));
				user.setPassword(rs.getString("password"));
				user.setAvatar(rs.getString("avatar"));
				list.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		helper.close();
		return list;
	}

}
